Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Typecasting

Upcasting and downcasting

Upcasting and downcasting

Upcasting and downcasting are two types of typecasting in Java. Upcasting is the process of assigning an object of a subclass to a variable of a superclass. Downcasting is the process of assigning an object of a superclass to a variable of a subclass. Upcasting is always safe, while downcasting is not always safe. Here is an example of upcasting:
Java typecasting type - upcasting example
public class Main { public static void main(String[] args) { Animal animal = new Dog(); animal.speak(); // Prints "I am an animal" } } class Animal { public void speak() { System.out.println("I am an animal"); } } class Dog extends Animal { public void bark() { System.out.println("Woof!"); } }

Output

I am an animal
In this code, the variable animal is declared as an Animal object. However, the object that is actually created is a Dog object. This is allowed because Dog is a subclass of Animal. Downcasting is not always safe because it is possible to assign an object of a superclass to a variable of a subclass that does not have all of the same methods and properties. Here is an example of downcasting that is not safe:
Java typecasting type - downcasting example
public class Main { public static void main(String[] args) { Animal animal = new Dog(); Dog dog = (Dog) animal; // This is not safe! dog.bark(); // This will cause a ClassCastException } } class Animal { public void speak() { System.out.println("I am an animal"); } } class Dog extends Animal { public void bark() { System.out.println("Woof!"); } }

Output

Woof!
In this code, the variable dog is declared as a Dog object. However, the object that is actually assigned to dog is an Animal object. This is not allowed because Dog has a method called bark() that Animal does not have. To make downcasting safe, you can use the instanceof operator to check if the object is an instance of the desired subclass. Here is an example of downcasting that is safe:
Java instanceOf for downcasting
public class Main { public static void main(String[] args) { Animal animal = new Dog(); if (animal instanceof Dog) { Dog dog = (Dog) animal; dog.bark(); } } } class Animal { public void speak() { System.out.println("I am an animal"); } } class Dog extends Animal { public void bark() { System.out.println("Woof!"); } }

Output

Woof!
In this code, the instanceof operator is used to check if the object animal is an instance of the Dog class. If it is, then the object is cast to a Dog object and the bark() method is called. Upcasting and downcasting are an important part of object-oriented programming in Java. They can be used to make code more readable and maintainable. However, it is important to be aware of the limitations of downcasting and to use it carefully. Here are some additional details about upcasting and downcasting in Java: - Upcasting is always safe because the subclass inherits all of the methods and properties of the superclass. - Downcasting is not always safe because the subclass may not have all of the methods and properties of the superclass. - To make downcasting safe, you can use the instanceof operator to check if the object is an instance of the desired subclass. - It is important to understand the difference between upcasting and downcasting in Java. By understanding these concepts, you can write code that is more readable, maintainable, and efficient.

  📌TAGS

★Upcasting ★ downcasting ★ typecasting ★ typecasting example

Tutorials